<?php

/**
 * 
 * Main controller. Deals with authentication, choice
 * of group, choice of section.
 * @author Théo Chamley
 *
 */
class IndexController extends CodeBar_AbstractController {

	/**
	 * 
	 * Login action
	 */
	public function indexAction() {
		$this->jQueryDisable();
		/*
		 * When you deconnect yourself, you end up here,
		 * so we destroy the former user.
		 */
		$this->session()->user = NULL;
		
		$this->_helper->layout->setLayout('layout_login');
		$this->view->headTitle("Connexion");
		$this->view->headLink()->appendStylesheet('/css/stylesheet_login.css');
		
		/*
		 * To display the version used.
		 */
		$bootstrap = $this->getInvokeArg('bootstrap');
		$config = $bootstrap->getOptions();
		$this->view->version = $config['version']['app'];

		/*
		 * New authentication form
		 */
		$form = new Application_Form_AuthForm();
		$form->send->setLabel('Connexion');
		$this->view->form = $form;
		if($this->getRequest()->isPost()) {
			$formData = $this->getRequest()->getPost();
			if($form->isValid($formData)) {
				$login = $form->getValue('login');
				$pwd = $form->getValue('pwd');

				$user = new Application_Model_User($login,$pwd);
				/*
				 * We check if the username/password is OK.
				 */
				$succes = $user->Authentification();
				$this->view->user = $user;//for tests
				if($succes) {
					/*
					 * If the username/password is OK, the user is
					 * redirected to choose the group to work in.
					 */
					$this->session()->user = $user;
					$this->_helper->redirector('groupchoice');
				} else {
					$this->view->error = "Login/password invalide(s)";
				}
			} else {
				$form->populate($formData);
			}
		}
	}

	/**
	 * 
	 * Action to choose the group.
	 */
	public function groupchoiceAction() {
		$this->view->headTitle("Choix du groupe");
		/*
		 * JS
		 */
		$this->view->headScript()->appendFile('/js/Groupchoice.js','text/javascript');
		
		/*
		 * CSS
		 */
		$this->view->headLink()->appendStylesheet('/css/site.css');

		$g = $this->session()->user->getGroupes();
		
		if(!($this->session()->user->connected)) {
			/*
			 * If the user is not connected, we redirect him
			 * to the login page.
			 */
			$this->_helper->redirector('index');
		} else if(count($g) == 0) {
			$this->view->message = 'Vous n\’êtes pas exceptionnels, vous n\’êtes pas un flocon de neige merveilleux et unique, vous êtes fait de la même substance organique pourrissante que tout le reste, nous sommes la merde de ce monde prête à servir à tout, nous appartenons tous au même tas d\’humus en décomposition. <br/>Tyler Durden<br/><i>Fight Club</i><br/>Vous n\'êtes associés à aucun groupe.';
		} else if(count($g) == 1) {
			/*
			 * If the user has only one group, then he is
			 * redirected to the group's page.
			 */
			$g[0]->getByName($g[0]->getName());
			$g[0]->getInfosProduits();
			$this->session()->user->setGroupeActif($g[0]);
			$this->_helper->redirector('modulechoice');
		} else {
			/*
			 * If the user has several groups, he has to choose one.
			 */
			$form = new Application_Form_GroupChoiceForm();
			$this->view->form = $form;
			if($this->getRequest()->isPost()) {
				$formData = $this->getRequest()->getPost();
				if($form->isValid($formData)) {
					$gactif = new Application_Model_Groupe();
					foreach($g as $h) {
						/*
						 * We look for the chosen group.
						 */
						if($form->getElement(str_replace(' ','',$h->getName()))->isChecked())
						$gactif=$h;
					}
					$gactif->getByName($gactif->getName());
					if($gactif->getID() != 1) {
						/*
						 * We process the infos of the products of this group
						 * to avoid doing it later (we do it once per session).
						 */
						$gactif->getInfosProduits();
					}
					$this->session()->user->setGroupeActif($gactif);
					/*
					 * We redirect to the page for choosing a module.
					 */
					$this->_helper->redirector('modulechoice');
				} else {
					$this->_helper->redirector('index');
				}
			}
		}
	}

	/**
	 * 
	 * Action to choose a module.
	 */
	public function modulechoiceAction() {
		$this->view->headScript()->appendFile('/js/Permissionchoice.js','text/javascript');

		/*
		 * Security...
		 */
		if(!($this->session()->user->connected)) {
			$this->_helper->redirector('index');
		} else if($this->session()->user->getGroupeActif()->getName() == 'administration') {
			$this->_helper->redirector('index','admin');
		}
		/*
		 * End of security...
		 */
		else {
			//-------head-----------------
			$this->view->headLink()->appendStylesheet('/css/stylesheet_module.css');
			//-------end of head----------
			$this->view->user = $this->session()->user;
			$roles = $this->session()->user->getRoles();
			$permissions = array();
			$perms = array();
			foreach($roles as $r) {
				$perms = $r->getPermissions();
				foreach($perms as $p) {
					$permissions[] = $p;
				}
			}
			
			/*
			 * We check the permissions of the user to see
			 * to which modules he has access to.
			 */
			$permissions = array_unique($permissions);
			$clearances = array();
			if(in_array('Vente',$permissions)) {$c['nom'] = 'Vente'; $c['label'] = '1 : Vente'; $clearances[] = $c;}
			if(in_array('GestionCarte',$permissions)) {$c['nom'] = 'GestionCarte'; $c['label'] = '2 : Gestion de Carte'; $clearances[] = $c;}
			if(in_array('GestionPerso',$permissions)) {$c['nom'] = 'GestionPerso'; $c['label'] = '3 : Gestion Perso'; $clearances[] = $c;}
			if(in_array('Historique',$permissions)) {$c['nom'] = 'Historique'; $c['label'] = '4 : Historique'; $clearances[] = $c;}
			if(in_array('PrixProduit',$permissions)) {$c['nom'] = 'PrixProduit'; $c['label'] = '5 : Prix et Produits'; $clearances[] = $c;}
			if(in_array('GestionGroupe',$permissions)) {$c['nom'] = 'GestionGroupe'; $c['label'] = '6 : Gestion Groupe'; $clearances[] = $c;}
			$this->session()->permissions = $clearances;
			$this->session()->per = $permissions;

			$form = new Application_Form_PermissionChoiceForm();
			$this->view->form = $form;
			if($this->getRequest()->isPost()) {
				$formData = $this->getRequest()->getPost();
				$permissionactive = 0;
				if($form->isValid($formData)) {
					foreach($permissions as $p) {
						if($form->getElement($p)->isChecked()) {
							$permissionactive = $p;
						}
					}
					
					/*
					 * We redirect the user to the chosen module.
					 */
					$this->session()->permissionactive = $permissionactive;
					if($permissionactive == "Vente") {$this->_helper->redirector('index','vente');}
					if($permissionactive == "Historique") {$this->_helper->redirector('index','historique');}
					if($permissionactive == "PrixProduit") {$this->_helper->redirector('index','prixproduit');}
					if($permissionactive == "GestionCarte") {$this->_helper->redirector('gestion','gestioncarte');}
					if($permissionactive == "GestionGroupe") {$this->_helper->redirector('index','gestiongroupe');}
					if($permissionactive == "GestionPerso") {$this->_helper->redirector('index','gestionperso');}
				}
			}

		}
	}
}

